﻿using System;
using System.Collections.Generic;
using System.Text;
using LBoL.Base;
using LBoL.Base.Extensions;
using LBoL.ConfigData;
using LBoL.Core;
using LBoL.Core.Battle;
using LBoL.Core.Battle.BattleActions;
using LBoL.Core.Cards;
using LBoL.Core.StatusEffects;
using LBoL.Core.Units;
using LBoL.EntityLib.Cards.Neutral.Blue;
using LBoLEntitySideloader.Attributes;
using ProbeMod.Cards.Template;

namespace ProbeMod.Cards
{
    public sealed class ProbeStasisWardDef : ProbeCardTemplate
    {
        public override CardConfig MakeConfig()
        {
            var config = GetCardDefaultConfig();

            config.TargetType = TargetType.Nobody;
            config.Type = CardType.Skill;
            config.Rarity = Rarity.Uncommon;

            config.Colors = new List<ManaColor>() { ManaColor.Blue };
            config.Cost = new ManaGroup() { Blue = 2 ,Any=2 };
            config.UpgradedCost = new ManaGroup() { Blue = 2, Any=1 };
            config.AutoPerform = false;

            config.Value1 = 2;

            config.RelativeEffects = new List<string>() { "ProbeTrapKeywordSe" };
            config.UpgradedRelativeEffects = new List<string>() { "ProbeTrapKeywordSe" };
            config.RelativeCards = new List<string>() { "ProbeStasisWardDeployed" };
            config.UpgradedRelativeCards = new List<string>() { "ProbeStasisWardDeployed" };

            config.Index = CardIndexGenerator.GetUniqueIndex(config);

            return config;
        }
    }

    /// <summary>
    /// 静置结界
    /// 部署静置结界
    /// 在手中：
    ///     回合开始时，取消部署并弃掉该牌。
    ///     若主角受到伤害，获得2层天衣无缝，并放逐自身
    /// </summary>
    [EntityLogic(typeof(ProbeStasisWardDef))]
    public sealed class ProbeStasisWard : ProbeCard
    {
        public override bool RemoveFromBattleAfterPlay => false;

        protected override IEnumerable<BattleAction> Actions(UnitSelector selector, ManaGroup consumingMana, Interaction precondition)
        {
            //  消耗自身，给一张已部署的结界
            yield return new AddCardsToHandAction(Library.CreateCard<ProbeStasisWardDeployed>(IsUpgraded));
            yield return new RemoveCardAction(this);
        }
    }

    public sealed class ProbeStasisWardDeployedDef : ProbeCardTemplate
    {
        public override CardConfig MakeConfig()
        {
            var config = GetCardDefaultConfig();

            config.TargetType = TargetType.Nobody;
            config.Type = CardType.Skill;
            config.Rarity = Rarity.Uncommon;
            config.IsPooled = false;
            config.Revealable = true;

            config.Colors = new List<ManaColor>() { ManaColor.Blue };
            config.Cost = ManaGroup.Empty;
            config.UpgradedCost = ManaGroup.Empty;
            config.AutoPerform = true;

            config.Keywords = Keyword.Retain | Keyword.Forbidden;
            config.UpgradedKeywords = Keyword.Retain | Keyword.Forbidden;

            config.RelativeEffects = new List<string>() { "ProbeTrapKeywordSe" };
            config.UpgradedRelativeEffects = new List<string>() { "ProbeTrapKeywordSe" };

            config.Value1 = 2;

            config.Index = CardIndexGenerator.GetUniqueIndex(config);

            return config;
        }
    }

    /// <summary>
    /// 已部署静置结界
    /// 
    /// 不应该出现在池子里
    /// </summary>
    [EntityLogic(typeof(ProbeStasisWardDeployedDef))]
    public sealed class ProbeStasisWardDeployed : ProbeCard
    {
        public override bool RemoveFromBattleAfterPlay => true;

        protected override void OnEnterBattle(BattleController battle)
        {
            ReactBattleEvent(base.Battle.Player.TurnStarted, OnPlayerTurnStarting);
            ReactBattleEvent(base.Battle.Player.DamageReceived, OnPlayerDamageTaking);
        }

        private IEnumerable<BattleAction> OnPlayerDamageTaking(DamageEventArgs args)
        {
            if (args.DamageInfo.Damage > 0f && !base.Battle.BattleShouldEnd)
            {
                //  受到伤害，给2层天衣无缝，消耗自身
                if (base.Zone == CardZone.Hand)
                {
                    yield return BuffAction<Invincible>(0,Value1);
                    yield return new ExileCardAction(this);
                }
            }
        }

        private IEnumerable<BattleAction> OnPlayerTurnStarting(UnitEventArgs args)
        {
            if (!base.Battle.BattleShouldEnd && base.Zone == CardZone.Hand)
            {
                // 回合开始，消耗自身，转化成目标卡牌
                yield return new AddCardsToDiscardAction(Library.CreateCard<ProbeStasisWard>(IsUpgraded));
                yield return new RemoveCardAction(this);
            }
        }

    }
}
